package com.example.park2212.repair

import android.content.Intent
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.RepairBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.net.Const
import com.example.park2212.R
import com.example.park2212.databinding.ActivityRepairAddBinding
import com.example.park2212.park.ParkPageAdapter
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/repair/RepairAddActivity")
class RepairAddActivity : BaseActivity<ActivityRepairAddBinding,RepairViewModel>() {
    val repairImgAdapter by lazy { RepairImgAdapter() }
    var area = "A区停车场"
    override fun initView() {

        binding.headRepairAdd.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("添加维修")
            headerAdd.setImageResource(R.drawable.ic_action_ok)
            headerAdd.setOnClickListener {
                val phone = binding.repairaddPhone.text.toString()
                val remark = binding.repairaddRemark.text.toString()
                val user = binding.repairaddUser.text.toString()
                val timeMillis = System.currentTimeMillis()
                val item = RepairBeanItem(
                    phone, remark, "", null,
                    "WX${viewModel.getDateTimeString(timeMillis)}",
                    "待处理", area,user,viewModel.getDateTime(timeMillis),
                    SPUtils.getInstance().getInt(Const.UID))
                viewModel.addRepair(item)
            }
        }

        val arr = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arr)
        binding.repairaddLocarion.adapter = adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.repairaddLocarion.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area = arr[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }

        binding.repairaddUser.setText(SPUtils.getInstance().getString("username"))


        binding.repairaddImg.setOnClickListener {
            if (repairImgAdapter.data.size<4){
                PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }else{
                ToastUtils.showLong("最多只能上传四张图片")
            }

        }

        binding.repairaddRv.apply {
            this.adapter = repairImgAdapter
            layoutManager = LinearLayoutManager(this@RepairAddActivity,RecyclerView.HORIZONTAL,false)
        }

    }

    override fun initData() {
        viewModel.addRepairSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.addData(bean?.path!!)
            repairImgAdapter.notifyDataSetChanged()
        }
    }

}